<?php
	abstract class BaseController{
		protected $view;
		public $template_data = array();
		public $security_level = 0;
		
		public function performActions($in_params){
			if ($this->security_level != 0 && (!isset($_SESSION['user']) || $_SESSION['user']['security_level'] < $this->security_level)){
				throw new Exception('Neautorizovaný prístup');
			}	
			if(isset($_REQUEST['action'])){
				// TODO security
				$action = 'action_'.$_REQUEST['action'];
				if(method_exists($this, $action)){
					return $this->$action($in_params);
				} else {
					general::debug("Action not found in ".get_class($this).".php", $action);
				}
			}
		}
		
		abstract function getData($in_params);
		
		public function getContent($in_params){
			//do some security checks
			
			// perform actions is called in main.php to view changes in all templates
			// $this->performActions($in_params);
			if ($this->security_level != 0 && (!isset($_SESSION['user']) || $_SESSION['user']['security_level'] < $this->security_level)){
				throw new Exception('Neautorizovaný prístup');
			}
			
			if (isset($GLOBALS['action_results_'.get_class($this)])){
				$this->template_data = array_merge($GLOBALS['action_results_'.get_class($this)], $this->template_data);
			}
			// get data for template
			$data = $this->getData($in_params);
			if(is_array($data)){
				$this->template_data = array_merge($data, $this->template_data);
			}
			// get proper view
			if (empty($this->view)){
				$this->view = $in_params['view'];
			}
			// if it's not a file
			if(strpos($this->view,'.') === false){
				$this->view .= '.tpl';	
			}	
			// return template
			$my_smarty = new my_smarty();
			$my_smarty->assign('data', $this->template_data);
			return $my_smarty->fetch($this->view);
		}
	}
?>